package com.chatdating.redis;


import com.chatdating.entity.constants.Constants;
import com.chatdating.entity.dto.ContactDto;
import com.chatdating.entity.pojo.TockenUserInfo;
import com.chatdating.entity.vo.LoginInVo;
import com.chatdating.utils.RedisUtil;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class RedisComponet {

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private RedissonClient redissonClient;

    /**
     * 获取token信息
     *
     * @param token
     * @return
     */
    public LoginInVo getTokenUserInfoDto(String token) {
        LoginInVo tokenUserInfoDto = (LoginInVo) redisUtil.get(Constants.REDIS_KEY_WS_TOKEN + token);
        return tokenUserInfoDto;
    }

    public LoginInVo getTokenUserInfoDtoByUserId(String userId) {
        String token = (String) redisUtil.get(Constants.REDIS_KEY_WS_TOKEN_USERID + userId);
        return getTokenUserInfoDto(token);
    }

    public void saveTokenUserInfoDto(LoginInVo LoginInVo) {
        //根据tocken存放tokenDTO信息
        redisUtil.setex(Constants.REDIS_KEY_WS_TOKEN + LoginInVo.getTocken(), LoginInVo, Constants.REDIS_KEY_EXPIRES_DAY * 2);
        //根据 userid 存放 tocken
        redisUtil.setex(Constants.REDIS_KEY_WS_TOKEN_USERID + LoginInVo.getUsername(), LoginInVo.getTocken(), Constants.REDIS_KEY_EXPIRES_DAY * 2);
    }
    //保存最后心跳时间
    public void saveUserHeartBeat(String userId) {
        redisUtil.setex(Constants.REDIS_KEY_WS_USER_HEART_BEAT + userId, System.currentTimeMillis(), Constants.REDIS_KEY_EXPIRES_HEART_BEAT);
    }
    public void cleanUserContact(String userId) {
        redisUtil.delete(Constants.REDIS_KEY_USER_CONTACT + userId);
    }
    //添加缓存联系人
    public void addUserContactBatch(String username, List<ContactDto> contactIdList) {
        redisUtil.lpushAll(Constants.REDIS_KEY_USER_CONTACT + username, contactIdList, Constants.REDIS_KEY_TOKEN_EXPIRES);
    }
    public List<LoginInVo> getUserContactList(String userId) {
        return redisUtil.getQueueList(Constants.REDIS_KEY_USER_CONTACT + userId);
    }
    //删除用户心跳
    public void removeUserHeartBeat(String userId) {
        redisUtil.delete(Constants.REDIS_KEY_WS_USER_HEART_BEAT + userId);
    }

}